package com.lianxian.xindong.me.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.android.material.appbar.AppBarLayout;
import com.hxb.library.base.BaseActivity;
import com.hxb.library.di.component.AppComponent;
import com.hxb.library.utils.HxbUtils;
import com.lianxian.xindong.commonres.adapter.MyPagerTitleAdapter;
import com.lianxian.xindong.commonsdk.core.RouterHub;
import com.lianxian.xindong.me.R;
import com.lianxian.xindong.me.R2;
import com.lianxian.xindong.me.di.component.DaggerFriendDetailComponent;
import com.lianxian.xindong.me.mvp.contract.FriendDetailContract;
import com.lianxian.xindong.me.mvp.presenter.FriendDetailPresenter;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;

import static com.hxb.library.utils.Preconditions.checkNotNull;


/**
 * @作者：hexingbo
 * @时间：07/07/2023
 * @描述：朋友的信息界面（基本信息，自拍作品，探店作品，动态）
 */
@Route(path = RouterHub.ME_FRIENDDETAILACTIVITY)
public class FriendDetailActivity extends BaseActivity<FriendDetailPresenter> implements FriendDetailContract.View {

    @Inject
    ArrayList<Fragment> mFragments;
    @Inject
    String[] mTitles;
    @Inject
    MyPagerTitleAdapter mAdapter;

    @BindView(R2.id.toolbar)
    Toolbar mToolbar;
    @BindView(R2.id.tab_menu)
    SlidingTabLayout mTabLayout;
    @BindView(R2.id.appBarLayout)
    AppBarLayout appBarLayout;
    @BindView(R2.id.infoPhone_Fragment_pager)
    ViewPager mViewPager;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerFriendDetailComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_friend_detail; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle("XXXXXX");
        TabLayout();
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.BaseOnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                // 导航栏为固定颜色时：
                mToolbar.setBackgroundColor(changeAlpha(getResources().getColor
                        (R.color.title_bg_color), Math.abs(verticalOffset * 1.0f) / appBarLayout.getTotalScrollRange()));
//                 导航栏有特有的背景时：直接改变透明度就行
//                mToolbar.setAlpha(Math.abs(verticalOffset * 1.0f) / appBarLayout
//                        .getTotalScrollRange());
                //mToolbar.setBackgroundResource(R.mipmap.ranking_logo);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        HxbUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        HxbUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    /**
     * 根据百分比改变颜色透明度
     */
    public int changeAlpha(int color, float fraction) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        int alpha = (int) (Color.alpha(color) * fraction);
        return Color.argb(alpha, red, green, blue);
    }

    /**
     * TabLayout
     * 初始化
     */
    private void TabLayout() {
        //预加载
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        mTabLayout.setViewPager(mViewPager);
        mTabLayout.onPageSelected(0);
    }

    @Override
    public Context getContext() {
        return this;
    }
}
